/*
 *   Copyright 2008 Marcos Silva Pereira
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */            
class ProfessorController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!(params.max?.toInteger() > 0)) params.max = 10
        [ professorList: Professor.list( params ) ]
    }

    def show = {
        def professor = Professor.get( params.id )

        if(!professor) {
            flash.message = "Professor not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ professor : professor ] }
    }

    def delete = {
        def professor = Professor.get( params.id )
        if(professor) {
            professor.delete()
            flash.message = "Professor ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Professor not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def professor = Professor.get( params.id )

        if(!professor) {
            flash.message = "Professor not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ professor : professor ]
        }
    }

    def update = {
        def professor = Professor.get( params.id )
        if(professor) {
            professor.properties = params
            if(!professor.hasErrors() && professor.save()) {
                flash.message = "Professor ${params.id} updated"
                redirect(action:show,id:professor.id)
            }
            else {
                render(view:'edit',model:[professor:professor])
            }
        }
        else {
            flash.message = "Professor not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def professor = new Professor()
        professor.properties = params
        return ['professor':professor]
    }

    def save = {
        def professor = new Professor(params)
        if(!professor.hasErrors() && professor.save()) {
            flash.message = "Professor ${professor.id} created"
            redirect(action:show,id:professor.id)
        }
        else {
            render(view:'create',model:[professor:professor])
        }
    }
}
